package com.zx.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.http.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

// import org.springframework.data.domain.Page;
// import org.springframework.data.domain.PageRequest;
// import org.springframework.data.domain.Pageable;
// import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMethod;

import com.base.web.common.DeleteVo;
import com.base.web.common.JsonVo;
import com.zx.web.entity.Drawing;
import com.zx.web.service.IDrawingService;

/**
 * Controller：图纸.<Drawing, String> 
 * @author Ken
 * @since 2017-04-23 22:41
 * @version v1.0.0
 */
@Controller
@RequestMapping("/drawing")
public class DrawingController{

    protected static Logger LOG = LoggerFactory.getLogger(Drawing.class);
    
	@Autowired
	private IDrawingService drawingService;
	
    /**
     * get 时 跳转到首页页面
     */
	@RequestMapping(value = {"/","/save","/update","/delete"}, method = { RequestMethod.GET })
	protected String checkGet(Drawing obj, Model model) {
		return main(model);
	}

	/**
	 * 跳转到首页面. 
	 */
	@RequestMapping(method = { RequestMethod.GET, RequestMethod.POST })
	protected  String main(Model model) { 
		return "drawing";
	}

	/**
	 * 跳转到更新页面.
	 * 
	 * @param id 实体对象的主键 
	 * @return 查看页面
	 */
	@RequestMapping(value = "view/{id}", method = RequestMethod.GET)
	protected String view(@PathVariable String id, Model model) {
		Drawing objDb = drawingService.findOne(id);
		model.addAttribute("obj", objDb);
		return "drawing-view";
	}

	/**
	 * 跳转到新增页面.
	 * 
	 * @param model
	 *            a holder for model attributes 数据
	 * @return 新增页面
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	protected String add(Model model) {
		model.addAttribute("obj", new Drawing());
		return "drawing-add";
	}

	/**
	 * 跳转到更新页面. 
	 * @return 更新页面
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	protected String edit(@PathVariable String id, Model model) {
		Drawing objDb = drawingService.findOne(id);
		model.addAttribute("obj", objDb);
		return "drawing-update";
	}

	/**
	 * 根据传入的数据ids,来删除对象.
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
	// @ResponseStatus(value = HttpStatus.OK) , headers = { "Content-type=application/json" }
	@ResponseBody
	protected JsonVo remove(@RequestBody DeleteVo<String> dvo) {
		try {
			if (dvo != null && dvo.getIds() != null && dvo.getIds().size() > 0) {
				List<Drawing> tts = drawingService.findAll(dvo.getIds());
				if (tts != null && !tts.isEmpty()) {
					drawingService.delete(tts);
				}
			}
			return new JsonVo(true);
		} catch (Exception e) {
			LOG.error("remove error ", e);
			return new JsonVo(false, "删除出现异常");
		}
	}

}
